import {
  PersonModel
} from '../../models/personal.js'
import { config } from '../../config.js'
let personModel = new PersonModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      FName: '',
      FPhoto: '',
      FWorkTime: '',
      FWorkplace: '',
      FEmpStatus: '',
      FRemark: '',
      FMobile: ''
    },
    photo: '',
    name: '',
    genders: [{
      name: '男',
      value: '1'
    }, {
      name: '女',
      value: '2'
    }],
    genderIndex: 0,
    status: [{
      name: '在岗求职',
      value: '1'
    }, {
      name: '正在上班',
      value: '2'
    }, {
      name: '待业求职',
      value: '3'
    }],
    statusIndex: 0,
    workDate: '',
    workPlace: '',
    mobile: '',
    otherDes:'',
  },

  genderSelector(e) {
    this.setData({
      genderIndex: e.detail.value
    })
  },

  dateSelector(e) {
    this.setData({
      workDate: e.detail.value
    })
  },

  statusSelector(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },

  pickPhoto() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          photo: res.tempFilePaths[0]
        })
      }
    })
  },

  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  
  workPlaceInput(e) {
    this.setData({
      workPlace: e.detail.value
    })
  },

  otherDesInput(e) {
    this.setData({
      otherDes: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '编辑个人信息'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    this.setData({
      workDate: year + '-' + month + '-' + day,
      mobile: options.mobile
    })
    if (options.fromPage == 'me') {
      this.requestPersonData()
    }
  },

  requestPersonData() {
    personModel.getPersonalData()
      .then(res => {
        var tempGdIdx, tempStatusIdx
        if (res.data.data.records.FGender == '男') {
          tempGdIdx = 0
        } else {
          tempGdIdx = 1
        }
        for (var i = 0; i < this.data.status.length; i++) {
          if (this.data.status[i].value == res.data.data.records.FEmpStatus) {
            tempStatusIdx = i
          }
        }
        this.setData({
          info: res.data.data.records,
          genderIndex: tempGdIdx,
          statusIndex: tempStatusIdx,
          workDate: res.data.data.records.FWorkTime,
          workPlace: res.data.data.records.FWorkplace,
          photo: res.data.data.records.FPhoto,
          name: res.data.data.records.FName,
          otherDes: res.data.data.records.FRemark,
        })
      })
  },

  checkData() {
    if (!this.data.name) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写姓名'
      })
      return
    }
    if (!this.data.workPlace) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写工作地点'
      })
      return
    }
    wx.showLoading()
    var uuid = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/;
    if (uuid.test(this.data.photo)) {
      this.saveData()
    } else {
      this.submitPic()
    }
    
  },

  submitPic() {
    var that = this
    wx.uploadFile({
      url: `${config.api_base_url}api/Ecp.Picture.upload.jup`,
      filePath: that.data.photo,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
        'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
      },
      formData: {
        'user': 'test'
      },
      success: function (res) {
        that.setData({
          'info.FPhoto': JSON.parse(res.data).data.pictureId
        })
        that.saveData()
      },
      fail: function (e) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
      }
    })
  },

  saveData() {
    this.setData({
      'info.FName': this.data.name,
      'info.FGender': this.data.genders[this.data.genderIndex].value,
      'info.FWorkTime': this.data.workDate,
      'info.FEmpStatus': this.data.status[this.data.statusIndex].value,
      'info.FWorkplace': this.data.workPlace,
      'info.FRemark': this.data.otherDes
    })
    personModel.savePersonalData({
      args: {
        data: [this.data.info]
      }
    }).then(res => {
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/workExperience/workExperience',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})